/**

 * @author admin

 * 2014年7月16日

 */

package com.tmall.testgen.plugin.wizards.runinspring.listener;

import java.util.List;
import java.util.Map;

import org.eclipse.jface.action.Action;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;

import com.tmall.testgen.client.ClientDataCenter;
import com.tmall.testgen.client.commond.InfoReceiveListener;
import com.tmall.testgen.client.runinspring.RunInSpringUtil;
import com.tmall.testgen.common.bo.interact.back.InterceptInfo;
import com.tmall.testgen.common.util.Comparator;
import com.tmall.testgen.plugin.views.vo.SpringTree;
import com.tmall.testgen.plugin.views.vo.TreeParent;
import com.tmall.testgen.plugin.wizards.MyMessageDialog;
import com.tmall.testgen.plugin.wizards.runinspring.RunInSpringWizadPage;

public class AddTestBtnSelectListener implements SelectionListener,InfoReceiveListener{
	private RunInSpringWizadPage page;
	private Comparator comparator=new Comparator();
	public AddTestBtnSelectListener(RunInSpringWizadPage page){
		this.page=page;
		ClientDataCenter.getInstance().setListener(this);
	}
	
	private void addToRoot(TreeParent parent,InterceptInfo info){

        String nodeName=info.getClassName()+"."+info.getMethodName()+info.getMethodParamContent();
        
        SpringTree root = new SpringTree();
        root.setLeafNode(false);
        root.setValue(info);
        root.setName(nodeName+" ["+info.getExecuteTime()+"]");
        parent.addChild(root);
        
        SpringTree importParam = new SpringTree();
        importParam.setName("入参:");
        importParam.setLeafNode(false);
        root.addChild(importParam);
        
        Map<String, Object>  paramMap=info.getParamMap();
        if(paramMap!=null){
            for(String name:paramMap.keySet()){
                if(paramMap.get(name)==null)
                    continue;
                SpringTree sub = new SpringTree();
                sub.setLeafNode(true);
                sub.setValue(paramMap.get(name));
                sub.setClazz(paramMap.get(name).getClass());
                sub.setFieldName(name);
                sub.initName();
                importParam.addChild(sub);
            }
        }
        
        SpringTree exportParam = new SpringTree();
        exportParam.setName("出参:");
        exportParam.setLeafNode(false);
        root.addChild(exportParam);
        

        Object obj = info.getResult();
        if(obj!=null){
            SpringTree sub = new SpringTree();
            sub.setLeafNode(obj==null||RunInSpringUtil.isBasicType(obj.getClass()));
            sub.setValue(obj);
            sub.setClazz(obj.getClass());
            sub.setFieldName("result");
            sub.initName();
            exportParam.addChild(sub);
        }
        
        Display.getDefault().syncExec(new Runnable() {
            public void run() {
                page.getGenCodeTreeViewer().refresh();
            }
        }); 
        
        
    
	}
	@Override
	public void widgetSelected(SelectionEvent e) {
		
		InterceptInfo info=page.getCurrentInfo();
		
		if(page.getCurrentInfo()==null){
			MyMessageDialog.openWarnDialog(page.getShell(), "我觉得您应该先计算下结果再添加至单元测试会好些！");
			return ;
		}
		
		addToRoot(page.getGenCodeInterceptRoot(),info);
			
		
		ClientDataCenter.getInstance().addInterceptInfo(page.getCurrentInfo());
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {

	}
	
	
    @Override
    public void refreshTree() {
        
    }
    @Override
    public void addInterceptInfo(InterceptInfo info) {
        if(info.getType()==InterceptInfo.TYPE_MOCK){
            List<InterceptInfo> infoes= ClientDataCenter.getInstance().getInfoes();
            
            boolean needAdd=true;
            if(infoes!=null&&infoes.size()>0){
                for(InterceptInfo tem:infoes){
                    String methodShow=tem.getMethodName()+tem.getMethodParamContent();
                    String methodShow2=info.getMethodName()+info.getMethodParamContent();
                    if(methodShow.equals(methodShow2)){
                        List p1=tem.getParams();
                        List p2=info.getParams();
                        
                        if(comparator.compare(p1, p2)){
                            needAdd=false;
                            break;
                            
                        }
                    }
                }
            }
            if(needAdd)
                addToRoot(page.getGenCodeMockRoot(),info);
            
        }
        
    }
    @Override
    public void actionChecked(Action selectAction) {
        
    }
    @Override
    public void beginMonitor(boolean isStart) {
        
    }

}
